<?php   
// Option file required
//require_once($oh['path']."/modules/simpleform/date.functions.php");

// Interface function   
function oh_function_simpleform($m) {
    global $oh_object_simpleform;
    /*
    if (count($tokens)>0) {
    	if ($oh_object_simpleform->attual_form['name'] == $tokens[0]) {
    		if (isset($oh_object_simpleform->attual_form['fields'][$tokens[1]])) {
    			return $oh_object_simpleform->attual_form['fields'][$tokens[1]];
    		} else {
        		//oh_message("Called unloaded form field value: ".$tokens[1]);
        		return false;		
    		}
    	} else {
    		oh_message("Called unloaded form: ".$tokens[0]);	
    	}        	
    }*/
    return oh_this();        
} 

// Main class of module
class oh_class_simpleform { 
	public $attual_form;        
	function oh_class_simpleform() {
	}
	function oh_object_simpleform() {
		global $oh_object_simpleform;
		return $oh_object_simpleform;
	}
	// form
	function open($name, $action="", $method="", $attr="") {
		ob_start();
		$action = ($action=="") ? oh("url") : $action;
		$method = ($method=="") ? ("POST") : (strtoupper($method));
		$this->attual_form = array(
			'name'	 => $name,
			'method' => $method,
			'action' => $action,
			'attr'   => $attr,
			'fields' => array(),
			'count'	 => 0,
			'file'	 => 0,
		);
		$this->load_fields($_POST);
		return $this->oh_object_simpleform();
	}
	function close() {
		$fn = $this->attual_form['name'];
		if ($this->attual_form['file']>0) {
			$multipart = 'enctype="multipart/form-data"';
		} else {
			$multipart = "";
		}
		echo "\n".'<input type="hidden" name="onsubmit" value="'.$fn.'" />';	
		echo "\n".'<input type="hidden" name="onsubmitdata" value="" />';	
		echo "\n".'</form>';
		echo "\n";
		$ob = ob_get_clean();
		echo "\n";
		echo '<form name="'.$this->attual_form["name"].'" action="'.$this->attual_form["action"].'" method="'.$this->attual_form["method"].'" '.$this->attual_form["attr"].' '.$multipart.'>';
		echo $ob;
	}
	// input
	function text($name,$value=NULL,$attr="") {
		if (!isset($value)) {
			$value = $this->get_value($name);
		}			
		echo "\n";
		echo '<input type="text" name="'.$name.'" value="'.$value.'" '.$attr.' />';	
		return $this->oh_object_simpleform();
	}
	function password($name,$value="") {
		if (!isset($value)) {
			$value = $this->get_value($name);
		}			
		echo "\n";
		echo '<input type="password" name="'.$name.'" value="'.$value.'" />';	
		return $this->oh_object_simpleform();
	}
	function textarea($name,$value=NULL,$attr="") {
		if (!isset($value)) $value = $this->get_value($name);
		echo "\n";
		echo '<textarea name="'.$name.'" '.$attr.' />'.$value.'</textarea>';
		return $this->oh_object_simpleform();
	}
	function checkbox($name,$checked=false,$attr="") {
		if ($checked) {
			$checked = ' checked="checked" ';
		} else {
			$checked = "";
		}
		echo "\n";
		echo '<input type="checkbox" name="'.$name.'" '.$checked.' '.$attr.' />';	
		return $this->oh_object_simpleform();
	}
	function hidden($name,$value) {
		if (!isset($value)) {
			$value = $this->get_value($name);
		}			
		echo "\n";
		echo '<input type="hidden" name="'.$name.'" value="'.$value.'" />';	
		return $this->oh_object_simpleform();
	}
	function radio($name,$value=NULL,$attr="") {
		echo "\n";
		echo '<input type="radio" name="'.$name.'" value="'.$value.'" '.$attr.' />';	
		return $this->oh_object_simpleform();
	}
	function file($name=NULL,$value=NULL,$attr="") {
		$name = (!$name) ? ("data_".$this->attual_form['count']) : ($name);
		$this->attual_form['file']++;
		echo "\n";
		echo '<input type="file" name="'.$name.'" value="'.$value.'" '.$attr.' />';	
		return $this->oh_object_simpleform();
	}
	function button($name=NULL,$value=NULL,$attr="") {
		$name = (!$name) ? ("data_".$this->attual_form['count']) : ($name);
		echo "\n";
		echo '<input type="button" name="'.$name.'" value="'.$value.'" '.$attr.' />';	
		return $this->oh_object_simpleform();
	}
	function submit($value,$data="") {
		$fn = $this->attual_form['name'];
		$sf = "onsubmit_".$this->attual_form['name'];
		$dt = $data;
		$js = 'javascript:document.'.$fn.'.onsubmitdata.value=\''.$dt.'\';if(typeof('.$sf.')==\'function\'){'.$sf.'();}else{document.'.$fn.'.submit();}';
		echo "\n";			
		echo '<input type="button" name="smb" value="'.$value.'" onclick="'.$js.'" />';	
		return $this->oh_object_simpleform();
	}
	function select($name,$value,$options,$attr) {
		echo "\n";
		echo '<select name="'.$name.'" '.$attr.'>';
		foreach($options as $okey=>$ovalue) {
			if ($okey==$value) {
				echo '<option value="'.$okey.'" selected="selected">'.$ovalue.'</option>'."\n";
			} else {
				echo '<option value="'.$okey.'">'.$ovalue.'</option>'."\n";				
			}
		}
		echo '</select>';
		return $this->oh_object_simpleform();
	}
	// pseudo events
	function onsubmit($name) {
		$action = oh("url");
		$method = "POST";
		$this->attual_form = array(
			'name'	 => $name,
			'method' => $method,
			'action' => $action,
			'fields' => array(),
		);			
		if (isset($_POST['onsubmit'])&&($_POST['onsubmit']==$name)) {
			$this->load_fields($_POST);
			return true;
		} else {
			return false;
		}
	}		
	// loading & sanitize
	function load_fields($array) {
		foreach($array as $key=>$value) {
			$this->attual_form['fields'][$key] = $value;
		}
	}
	function get_value($name) {
		if (isset($_POST[$name])) {
			return $_POST[$name];
		} else {
			return "";	
		}
	}
	// other
	function label($text) {
		echo "\n";
		echo '<label>'.$text.'</label>';	
		return $this->oh_object_simpleform();
	}
	function br($n=1) {
		for($i=0;$i<$n;$i++) echo '<br/>';				
		return $this->oh_object_simpleform();
	}
	function sep($h="8px") {
		echo "\n".'<div style="height:'.$h.';"></div>';
		return $this->oh_object_simpleform();
	}
	function sp() {
		echo '<span>&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp</span>';
		return $this->oh_object_simpleform();
	}
	function nbsp() {
		echo '<span>&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp</span>';
		return $this->oh_object_simpleform();
	}
	// date/time managment
	function date($name,$value,$range,$lang="en",$attr="") {		
		global $oh;
		include($oh['path']."/modules/simpleform/data.php");	
		$value = strtotime($value);
		$value_day 		= date("j",$value);
		$value_month 	= date("n",$value);
		$value_year 	= date("Y",$value);
		
		$range 			= explode(":",$range);
		$years 			= new_years_array($range[0],$range[1]);
		
		$this->select("{$name}_day",	$value_day,		$days, 		$attr);
		$this->select("{$name}_month",	$value_month,	$months,	$attr);
		$this->select("{$name}_year",	$value_year,	$years,		$attr);
		return $this->oh_object_simpleform();
	}
}
